home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / APP / S-Z / The Regulator.cpt / The Regulator source / PB.c < prev    next >
Text File  |  1992-06-19  |  5KB  |  220 lines

  1. /*
  2. ** PB.c
  3. **
  4. ** PowerBook specific routines for setting & getting various sleep and rest values.
  5. ** of course, any of these can change at any time, and they aren't official
  6. ** nor from Apple.  I figured these out by disassembling code.  This stuff WILL
  7. ** change in the future.  Don't base any products on this, or you'll be very
  8. ** sorry (and DTS will laugh in your face…)  Don't ask Apple for help on this
  9. ** stuff… they have no further information they are willing to disclose.
  10. */
  11. #include "XPRAM.h"
  12. #include <Power.h>
  13. #include <GestaltEqu.h>
  14.  
  15. #ifdef THINK_C
  16. /* These are defined in MPW C 3.2.3, but not in Think C 5.0.2 */
  17. #define    gestaltPowerBook100        24    /* not defined in GestaltEqu.h */
  18. #define    gestaltPowerBook140        25    /* not defined in GestaltEqu.h */
  19. #define    gestaltPowerBook170        21    /* not defined in GestaltEqu.h */
  20. #endif
  21.  
  22. #define    kUnitsOfSleep        (60/15)        /* empirical from source */
  23. #define kMaxSysSleep        15*kUnitsOfSleep
  24. #define kMaxHDSleep            15*kUnitsOfSleep
  25. #define kByteSize            1
  26.  
  27. /* globals */
  28. Boolean    gOnBattery;        /* charger connection determines this */
  29. short    gFlagsLocation;
  30. Byte    gSysSleep;
  31. Byte    gHDSleep;
  32.  
  33. /* procedures declared in this module */
  34. OSErr    CheckForMains();
  35. void    SetPMgrDirty();
  36. Boolean    ToggleFlag(short, Byte, Boolean);
  37. Boolean    SetDontRest();
  38. Boolean    SetRest();
  39. Byte    GetUserSysSleep();
  40. Byte    GetUserHDSleep();
  41. void    SetSysSleep();
  42. void    SetHDSleep();
  43. void    InitPortableStuff();
  44.  
  45. /* CheckForMains
  46.  *
  47.  * This is the routine we invoke when called periodically.
  48.  * Check for battery status.  if we've recently been plugged in, modify
  49.  * the idle state.
  50.  */
  51.  
  52. OSErr
  53. CheckForMains()
  54. {
  55.     Byte    status;
  56.     Byte    power;
  57.     Boolean    currentlyConnected;
  58.     char    prFlags;
  59.     OSErr    err;
  60.     
  61.     err = BatteryStatus(&status, &power);
  62.     
  63.     currentlyConnected = status & chargerConnMask;
  64.     if (currentlyConnected != gOnBattery)
  65.     {
  66.         if (gOnBattery) {    /* we just got disconnected from the mains */
  67.             SetRest();
  68.             SetSysSleep(gSysSleep);
  69.             SetHDSleep(gHDSleep);
  70.         }
  71.         else {                    /* we just connected to the mains */
  72.             SetDontRest();
  73.             gSysSleep = GetUserSysSleep();
  74.             gHDSleep = GetUserHDSleep();
  75.             SetSysSleep(kMaxSysSleep);
  76.             SetHDSleep(kMaxHDSleep);
  77.         }
  78.         gOnBattery = currentlyConnected;
  79.     }
  80. }
  81.  
  82.  
  83.  
  84. /* Set the Power Manager's dirty byte whenever anything changes */
  85. void SetPMgrDirty()
  86. {
  87.         PMgrLocal(TOdirtyFlag) = 0xff; /* set the byte */
  88. }
  89.  
  90. /*
  91.  * Toggle a flag in prFlags
  92.  * Return the new state of the flag
  93.  */
  94. Boolean ToggleFlag(short flagPramAddr,Byte bit, Boolean newState)
  95.     {
  96.     Byte scratch;
  97.     
  98.     ReadPRAM(flagPramAddr, kByteSize, (char *)&scratch);    /* Read the current state */
  99.  
  100.     if (newState)
  101.         scratch |= bit;
  102.     else
  103.         scratch &= ~bit;
  104.  
  105.     SetPRAM(flagPramAddr, kByteSize, (char *)&scratch);            /* Put it back */
  106.     SetPMgrDirty();                                /* Tell PMgr to notice the change */
  107.     return newState;                            /* return the new state */
  108.     }
  109.  
  110. /*
  111.  * Set the "Don't Rest while plugged in" flag in PRAM
  112.  */
  113. Boolean SetDontRest()
  114. {
  115.     Boolean    result;
  116.     
  117.     result = ToggleFlag(gFlagsLocation,kPowerSave, true);
  118. }
  119.  
  120. /*
  121.  * Set the "Rest while plugged in" flag in PRAM
  122.  */
  123. Boolean SetRest()
  124. {
  125.     Boolean    result;
  126.     
  127.     result = ToggleFlag(gFlagsLocation,kPowerSave, false);
  128. }
  129.  
  130. /*
  131. ** GetUserSysSleep
  132. ** read the PRAM and return what the user set gSysSleep to be
  133. */
  134. Byte GetUserSysSleep()
  135. {
  136.     Byte    scratch;
  137.     
  138.     ReadPRAM(PRAM_BASE+prSysSleep_Offset, kByteSize, (char *)&scratch);
  139.     return scratch;
  140. }
  141.  
  142. /*
  143. ** GetUserHDSleep
  144. ** read the PRAM and return what the user set gHDSleep to be
  145. */
  146. Byte GetUserHDSleep()
  147. {
  148.     Byte    scratch;
  149.     
  150.     ReadPRAM(PRAM_BASE+prHDSleep_Offset, kByteSize, (char *)&scratch);
  151.     return scratch;
  152. }
  153.  
  154.  
  155. /*
  156. ** SetSysSleep
  157. ** read the PRAM and return what the user set gSysSleep to be
  158. */
  159. void SetSysSleep(newValue)
  160. Byte newValue;
  161. {
  162.     SetPRAM(PRAM_BASE+prSysSleep_Offset, kByteSize, (char *)&newValue);
  163.     SetPMgrDirty();                                /* Tell PMgr to notice the change */
  164. }
  165.  
  166. /*
  167. ** SetHDSleep
  168. ** read the PRAM and return what the user set gHDSleep to be
  169. */
  170. void SetHDSleep(newValue)
  171. Byte newValue;
  172. {
  173.     SetPRAM(PRAM_BASE+prHDSleep_Offset, kByteSize, (char *)&newValue);
  174.     SetPMgrDirty();                                /* Tell PMgr to notice the change */
  175. }
  176.  
  177. /*
  178. ** InitPortableStuff
  179. ** set globals so they work with the Portable model we have.
  180. ** We only work with the Portable, PowerBook100, PowerBook140, and PowerBook170
  181. ** (Heck, who knows what the future brings?)
  182. */
  183. void InitPortableStuff()
  184. {
  185.     Byte    status;
  186.     Byte    power;
  187.     OSErr    err;
  188.     long    gestaltData;
  189.     Boolean    knownComputer;
  190.  
  191.     Gestalt(gestaltPowerMgrAttr, &gestaltData);
  192.     
  193.     if (!(gestaltData & (1 << gestaltPMgrExists)) ) {
  194.         ExitToShell();
  195.     }
  196.  
  197.     Gestalt(gestaltMachineType, &gestaltData);
  198.     knownComputer = (gestaltData == gestaltPortable) 
  199.                     || (gestaltData == gestaltPowerBook100)
  200.                     || (gestaltData == gestaltPowerBook140) 
  201.                     || (gestaltData == gestaltPowerBook170);
  202.     
  203.     if (!knownComputer)
  204.         ExitToShell();
  205.  
  206.     err = BatteryStatus(&status, &power);
  207.     
  208.     gOnBattery = ~(status & chargerConnMask);    /* this ensures we'll do something */
  209.                                                 /* 1st time around */
  210.  
  211.     gSysSleep = GetUserSysSleep();
  212.     gHDSleep = GetUserHDSleep();
  213.  
  214.     Gestalt(gestaltMachineType, &gestaltData);
  215.     
  216.     if ((gestaltData == gestaltPortable) || (gestaltData == gestaltPowerBook100))
  217.         gFlagsLocation = PRAM_BASE+prFlags_Offset_Portable;
  218.     else
  219.         gFlagsLocation = PRAM_BASE+prFlags_Offset;
  220. }